package com.yijiaw.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.yijiaw.domain.Admin;
import com.yijiaw.service.AdminService;
import com.yijiaw.service.ProductService;
import com.yijiaw.service.ProducttypeService;

@Controller
@RequestMapping("/admin")
public class AdminController {
	
	@Autowired
	AdminService adminService;
	
	@Autowired
	ProductService productService;
	
	@Autowired
	ProducttypeService producttypeService;
	
	@RequestMapping(value = {"/login", "/", "index", "default"}, method = RequestMethod.GET)
	public String login() {
		return "admin/login";
	}
	
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login(@RequestParam String username, @RequestParam String password, HttpServletRequest request) {
		Admin admin = adminService.findAdmin(username, password);
		if (admin == null) {
			return "admin/loginError";
		}
		request.getSession().setAttribute("admin", admin);
		return "redirect:main";
	}
	
	@RequestMapping("/main")
	public String main() {
		return "admin/main";
	}
	
	@RequestMapping("/center")
	public String center() {
		return "admin/center";
	}
	
	@RequestMapping("/top")
	public String top() {
		return "admin/top";
	}
	
	@RequestMapping("/middel")
	public String middel() {
		return "admin/middel";
	}
	
	@RequestMapping("/left")
	public String left() {
		return "admin/left";
	}
	
	@RequestMapping("/tab")
	public String tab() {
		return "admin/tab";
	}
	
	@RequestMapping("/down")
	public String down() {
		return "admin/down";
	}
	
	@RequestMapping("/logoff")
	public String logoff() {
		return "redirect:login";
	}
	
	@RequestMapping(value = "/changePwd", method = RequestMethod.GET)
	public String changPwd() {
		return "admin/changePwd";
	}
	
	@RequestMapping(value = "/changePwd", method = RequestMethod.POST)
	public String changePwd(@ModelAttribute Admin admin, HttpServletRequest request) {
		adminService.saveAdmin(admin);
		request.getSession().setAttribute("admin", admin);
		
		return "admin/pwd_sucess";
	}
	
}
